﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MODULE_DANHMUC
{
    public partial class frm_danhmucchuhang : Form
    {
        int currenrowindex;
        danhmucDataContext context =cls_danhmuc.getcontext();

        public frm_danhmucchuhang()
        {
            InitializeComponent();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            
            

            DM_chuhang ch = new DM_chuhang();
            ch.machuhang = this.txtmachuhang.Text;
            ch.tenchuhang = this.txttenchuhang.Text;
            ch.diachi = this.txtdiachi.Text;
            ch.dienthoai = this.txtdienthoai.Text;
            ch.ghichu = this.txtghichu.Text;
            if (this.chkdoanhnghiep.Checked == true)
            {
                ch.doanhnghiep = true;
            }
            else
            {
                ch.doanhnghiep = false;
            }

            try
            {


                IEnumerable<DM_chuhang> list = from c in context.DM_chuhangs where c.machuhang.Equals(ch.machuhang) select c;
                if (list.Count() > 0)
                {
                    foreach (DM_chuhang item in list)
                    {
                        item.tenchuhang=ch.tenchuhang;
                        item.diachi = ch.diachi;
                        item.dienthoai = ch.dienthoai;
                        item.ghichu = ch.ghichu;
                        item.doanhnghiep = ch.doanhnghiep;

                    }

                }
                else
                {
                    context.DM_chuhangs.InsertOnSubmit(ch);
                }

                context.SubmitChanges();
                MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Không lưu được.Lỗi: " + ex.Message, "Thông báo", MessageBoxButtons.OK);
            }
            getdata();
        }
        private void getdata()
        {
            List<P_getmachuhangResult> mach = context.P_getmachuhang().ToList();
            if (mach.Count() > 0)
            {
                this.txtmachuhang.Text = mach.ElementAt(0).mach;
            }
            this.grdata.Rows.Clear();
           
           
            IEnumerable<DM_chuhang> list = context.DM_chuhangs.AsEnumerable();
            for (int i = 0; i < list.Count(); i++)
            {
                DM_chuhang item = list.ElementAt(i);
                this.grdata.Rows.Add();
                this.grdata.Rows[i].Cells["machuhang"].Value = item.machuhang;
                this.grdata.Rows[i].Cells["tenchuhang"].Value = item.tenchuhang;
                this.grdata.Rows[i].Cells["diachichuhang"].Value = item.diachi;



            }
        }
        private void getrowdata()
        {
            
            DM_chuhang ch = new DM_chuhang();
            if (currenrowindex < this.grdata.Rows.Count)
            {
                string machuhang = grdata.Rows[currenrowindex].Cells["machuhang"].Value.ToString();
                IEnumerable<DM_chuhang> list=from c in context.DM_chuhangs where c.machuhang.Equals(machuhang) select c;
                ch =list.ElementAt(0);
                
               
                this.txtmachuhang.Text = ch.machuhang;
                this.txttenchuhang.Text = ch.tenchuhang;
                this.txtdiachi.Text = ch.diachi;
                this.txtdienthoai.Text = ch.dienthoai;
                this.txtghichu.Text = ch.ghichu;
                if (ch.doanhnghiep == true)
                {
                    this.chkdoanhnghiep.Checked = true;
                }
                else
                {
                    this.chkdoanhnghiep.Checked = false;
                }
            }
            else
            {
                clear();
               
               
            }
        }
        private void clear()
        {
            this.txtmachuhang.Text = "";
            this.txttenchuhang.Text = "";
            this.txtdiachi.Text = "";
            this.txtdienthoai.Text = "";
            this.txtghichu.Text = "";
            this.chkdoanhnghiep.Checked = false;
        }
        private void frm_danhmucchuhang_Load(object sender, EventArgs e)
        {
            
            currenrowindex = 0;
            getdata();
            
        }

        private void grdata_Click(object sender, EventArgs e)
        {
            currenrowindex = this.grdata.CurrentRow.Index;
            this.grdata.Rows[currenrowindex].Selected = true;
            getrowdata();
        }

        private void btnext_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;
            if (currenrowindex < this.grdata.Rows.Count-1 )
            {
                currenrowindex = currenrowindex + 1;
            }

            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();

        }

        private void btlast_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;

            currenrowindex = this.grdata.Rows.Count - 1;


            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();
        }

        private void btprevious_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;
            if (currenrowindex > 0)
            {
                currenrowindex = currenrowindex - 1;
            }

            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();
        }

        private void btfist_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;

            currenrowindex = 0;


            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            try
            {
               
                DM_chuhang ch = new DM_chuhang();

                
                string machuhang = this.txtmachuhang.Text;
                IEnumerable<DM_chuhang> l = from c in context.DM_chuhangs where c.machuhang.Equals(machuhang) select c;



                // MessageBox.Show("Bạn chắc chắn muốn xóa?", "Xác nhận",MessageBoxButtons.YesNo);





                foreach (DM_chuhang i in l)
                {
                    context.DM_chuhangs.DeleteOnSubmit(i);
                }
                context.SubmitChanges();

                MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi không xóa được.Mã chủ hàng đã được sử dụng.");
               // MessageBox.Show("Không xóa được.Lỗi: " + ex.Message, "Thông báo", MessageBoxButtons.OK);
            }
            getdata();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void txtkeyword_TextChanged(object sender, EventArgs e)
        {
            this.grdata.Rows.Clear();

            string keyword = this.txtkeyword.Text;
            IEnumerable<DM_chuhang> list = from c in context.DM_chuhangs where c.tenchuhang.Contains(keyword) select c;
            for (int i = 0; i < list.Count(); i++)
            {
                DM_chuhang item = list.ElementAt(i);
                this.grdata.Rows.Add();
                this.grdata.Rows[i].Cells["machuhang"].Value = item.machuhang;
                this.grdata.Rows[i].Cells["tenchuhang"].Value = item.tenchuhang;
                this.grdata.Rows[i].Cells["diachichuhang"].Value = item.diachi;



            }
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      

        private void btAdd_Click(object sender, EventArgs e)
        {
            List<P_getmachuhangResult> mach = context.P_getmachuhang().ToList();
            if (mach.Count() > 0)
            {
                this.txtmachuhang.Text = mach.ElementAt(0).mach;
            }
            this.chkdoanhnghiep.Checked = false;
            this.txttenchuhang.Text = "";
            this.txtdiachi.Text = "";
            this.txtdienthoai.Text = "";
            this.txtghichu.Text = "";
        }

        private void btSave_Click_1(object sender, EventArgs e)
        {

        }

        
      

       
    }
}
